package service

import (
	"github.com/wenlng/go-captcha/captcha"
	"go-shop-admin/pkg/e"
	"go-shop-admin/pkg/go_captcha"
	"go-shop-admin/pkg/serializer"
)

//验证码

// CaptchaService 验证验证码
type CaptchaService struct {
	Key  string `form:"key" binding:"required" `
	Dots string `form:"dots" binding:"required" `
}

// GetVerifyCode 发送验证码
func (cs *CaptchaService) GetVerifyCode() *serializer.Response {
	//
	//return go_captcha.GetVerifyCode()

	capt := captcha.GetCaptcha()
	dots, b64, tb64, key, err := capt.Generate()
	if err != nil {
		return serializer.NewResponse(e.ErrorGetCaptcha, "GenCaptcha err", e.TipsErrorGetCaptcha, nil)
	}
	//写入缓存
	go go_captcha.WriteCache(dots, key)
	//fmt.Println(json.Marshal(dots)) 打印主点

	return serializer.NewResponse(e.Success, e.TipsOk, e.TipsSuccessGetCaptcha, map[string]any{
		"image_base64": b64,
		"thumb_base64": tb64,
		"captcha_key":  key,
	})

}

// CheckVerifyCode 验证验证码
func (cs *CaptchaService) CheckVerifyCode() *serializer.Response {
	if err := go_captcha.CheckVerifyCode(cs.Key, cs.Dots); err != nil {
		return serializer.NewResponse(e.ErrorCheckCaptcha, e.TipsNil, err.Error(), nil)
	}
	return serializer.NewResponse(e.Success, e.TipsOk, e.TipsSuccessCheckCaptcha, nil)
}
